﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;

namespace API_Service.DBHelper
{
    public class SqlCreator
    {
        private static string ConnectionString { get; set; }
        private static DataBaseType DBType  { get; set; }

        
        private static Dictionary<string, string> _connStrings = new Dictionary<string, string>();
        public static Dictionary<string, string> ConnectionStrings
        {
            get { return _connStrings; }
        }
        static SqlCreator()
        {
            ConnectionString = ConfigurationManager.ConnectionStrings["mesdb"].ConnectionString;
            _connStrings.Add("mesdb", ConnectionString);
            _connStrings.Add("yfdb", ConfigurationManager.ConnectionStrings["yfdb"].ConnectionString);

            DBType = DataBaseType.SqlServer;
        }

        public static IDbConnection Create()
        {
            IDbConnection dbConnection = Build(DBType);

            if (dbConnection != null)
            {
                dbConnection.ConnectionString = ConnectionString;
            }

            return dbConnection;
        }
        public static IDbConnection Create(DataBaseType dataBaseType)
        {
            IDbConnection dbConnection = Build(dataBaseType);

            if (dbConnection != null)
            {
                dbConnection.ConnectionString = ConnectionString;
            }

            return dbConnection;
        }
        public static IDbConnection Create(DataBaseType dataBaseType, string connectionString)
        {
            IDbConnection dbConnection = Build(dataBaseType);

            if (dbConnection != null)
            {
                dbConnection.ConnectionString = connectionString;
            }

            return dbConnection;
        }

        public static IDbConnection Create(bool Singleton)
        {
            IDbConnection dbConnection = BuildSingleton(DBType);

            if (dbConnection != null)
            {
                dbConnection.ConnectionString = ConnectionString;
            }

            return dbConnection;
        }
        public static IDbConnection Create(DataBaseType dataBaseType, bool Singleton)
        {
            IDbConnection dbConnection = BuildSingleton(dataBaseType);

            if (dbConnection != null)
            {
                dbConnection.ConnectionString = ConnectionString;
            }

            return dbConnection;
        }
        public static IDbConnection Create(DataBaseType dataBaseType, string connectionString, bool Singleton)
        {
            IDbConnection dbConnection = BuildSingleton(dataBaseType);

            if (dbConnection != null)
            {
                dbConnection.ConnectionString = connectionString;
            }

            return dbConnection;
        }

        private static IDbConnection Build(DataBaseType db)
        {
            IDbConnection dbConnection = null;

            switch (db)
            {
                case DataBaseType.SqlServer:
                    dbConnection = SqlServerCreator.NewConnection();
                    break;
                case DataBaseType.MySql:
                    dbConnection = MySqlCreator.NewConnection();
                    break;
                case DataBaseType.MSOracle:
                    dbConnection = MsOracleCreator.NewConnection();
                    break;
                case DataBaseType.ODPOrace:
                    break;
                default:
                    dbConnection = SqlServerCreator.NewConnection();
                    break;
            }

            return dbConnection;
        }
        private static IDbConnection BuildSingleton(DataBaseType db)
        {
            IDbConnection dbConnection = null;

            switch (db)
            {
                case DataBaseType.SqlServer:
                    dbConnection = SqlServerCreator.SingletonConnection();
                    break;
                case DataBaseType.MySql:
                    dbConnection = MySqlCreator.SingletonConnection();
                    break;
                case DataBaseType.MSOracle:
                    dbConnection = MsOracleCreator.SingletonConnection();
                    break;
                case DataBaseType.ODPOrace:
                    break;
                default:
                    dbConnection = SqlServerCreator.SingletonConnection();
                    break;
            }

            return dbConnection;
        }

    }
}